﻿using AutoDeployForSims.Common;
using AutoDeployForSims.From18To10Cfg.Model;
using AutoDeployForSims.From18To10Cfg.Utility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoDeployForSims.From18To10Cfg
{
    public class Write10CfgFrom18
    {
        public bool write18CFGService(String telCode)
        {
            bool isRight = true;
            GetInit18Config getInit18Config = new GetInit18Config();
            string file18Path = Path.GetDirectoryName(GlobalVari.CFGPath);
            getInit18Config.readAnalogExtend18(file18Path + @"/", telCode);
            getInit18Config.readAnalog18(file18Path + @"/", telCode);

            Dictionary<int, AnalogEnt18> analog18EntMap = getInit18Config.Analog18EntMap;
            Dictionary<int, AnalogEnt18> analogJY18EntMap = getInit18Config.AnalogJY18EntMap;
            Dictionary<int, AnalogEnt18> analogLL18EntMap = getInit18Config.AnalogLL18EntMap;
            Dictionary<int, AnalogExtend18> analogExtend18EntMap = getInit18Config.AnalogExtend18EntMap;

            Console.WriteLine("analog18Ent：" + analog18EntMap.Count());
            Console.WriteLine("analogJY18Ent：" + analogJY18EntMap.Count());
            Console.WriteLine("analogLL18Ent：" + analogLL18EntMap.Count());
            Console.WriteLine("analogExtend18Ent：" + analogExtend18EntMap.Count());

            CreateConfig18To10 createConfig18To10 = new CreateConfig18To10();

            try
            {
                // new Date()为获取当前系统时间，也可使用当前时间戳
                DateTime dt = DateTime.Now;
                //2005-11-5 0:00:00 
                string date = dt.Date.ToString("yyyy-MM-dd-") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();

                //DecimalFormat fnum = new DecimalFormat("0.00000");
                //string testPath = ".\\OutDir\\test\\";
                string testPath = ".\\SimulatonTools\\";
                testPath = Path.Combine(testPath, telCode + "模拟工具\\10配置库\\");
                //检查文件是否存在
                if (!System.IO.Directory.Exists(testPath))
                {
                    System.IO.Directory.CreateDirectory(testPath);
                }


                //写analog
                Dictionary<int, AnalogEnt> analogEntMap = createConfig18To10.CreateAnalog10Map(analog18EntMap, analogExtend18EntMap);
                String txtPath = testPath + "test-" + telCode + "-" + date + "-全体.txt";
                WriteFile(txtPath, analogEntMap);

                //绝缘
                Dictionary<int, AnalogEnt> analogJYEntMap = createConfig18To10.CreateAnalogJY10Map(analogJY18EntMap, analogExtend18EntMap);
                String txtJYPath = testPath + "test-" + telCode + "-" + date + "-JY.txt";
                WriteFile(txtJYPath, analogJYEntMap);
                //System.out.println("==jyEnd==");

                //漏流
                Dictionary<int, AnalogEnt> analogLLEntMap = createConfig18To10.CreateAnalogLL10Map(analogLL18EntMap, analogExtend18EntMap);
                String txtLLPath = testPath + "test-" + telCode + "-" + date + "-LL.txt";
                WriteFile(txtLLPath, analogLLEntMap);

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return false;
            }
            return isRight;
        }

        private void WriteFile(String txtPath, Dictionary<int, AnalogEnt> analogEntMap)
        {
            String str = "";
            string decimalFormat = "0.00000";//格式化设置
            //检查文件是否存在
            if (!System.IO.File.Exists(txtPath))
            {
                using (System.IO.FileStream fs = System.IO.File.Create(txtPath))
                {
                    
                }
            }

            WriteTXT.WriteToTXT(txtPath, "总路数=" + analogEntMap.Count() + "\r\n");
            //写

            foreach (var entry in analogEntMap)
            {
                //System.out.println(entry.Value.getAnalogMagnification());
                str = entry.Key + "=\t" + entry.Value.AnalogName + ",\t"
                        + entry.Value.AnalogFlag + ",\t"
                        + (entry.Key - 1) + ",\t"
                        + (entry.Value.AnalogType) + ",\t"

                        + entry.Value.AnalogRangeLower + ",\t"
                        + entry.Value.AnalogRangeUpper + ",\t"

                        + entry.Value.AnalogCoefficient + ",\t"
                        + entry.Value.AnalogLimitUpper + ",\t"
                        + entry.Value.AnalogLimitLower + ",\t"

                        + entry.Value.AnalogShuntLimit + ",\t"

                        + entry.Value.AnalogExtensionNum + ",\t"
                        
                        + entry.Value.AnalogMagnification.ToString(decimalFormat) + ",\t"
                        + entry.Value.MagnificationSwitch + ",\t"

                        + entry.Value.Reserve1 + ",\t"
                        + entry.Value.Reserve2 + ",\t"
                        + entry.Value.Reserve3 + ",\t"


                        + entry.Value.AnalogUnit + ",\t"

                        + entry.Value.Switch1 + ",\t"
                        + entry.Value.Switch2 + ",\t"
                        + entry.Value.Switch3 + ",\t"
                        + entry.Value.Switch4 + ",\t"
                        + entry.Value.Switch5 + ",\t"

                        + entry.Value.AlarmDelayTime + ",\t"
                        + entry.Value.AlarmRecoveryTime + ",\t"
                        + entry.Value.AnalogWaveFlag + ",\t"

                        + entry.Value.AnalogRefValue + ",\t"
                        + entry.Value.AnalogWaveRange + ",\t"
                        + entry.Value.AnalogWaveFrequency + ",\t"

                        + entry.Value.AnalogDuration + ",\t"
                        + entry.Value.AlarmName + ",\t"
                        + entry.Value.AnalogTuBianDuration + ",\t"

                        + entry.Value.AnalogGFLowerThreshold + ",\t"
                        + entry.Value.AnalogGFUpperThreshold + ",\t"
                        + entry.Value.AnalogGFFLUpperThreshold + ",\t"

                        + entry.Value.AnalogGFDigit + ",\t"
                        + entry.Value.PreFindTime + ",\t"
                        + entry.Value.NextFindTime + ",\t"
                        + entry.Value.TuBianDuration2 + ",\t"
                        + entry.Value.TuBianDuration3 + "\r\n";

                WriteTXT.WriteToTXT(txtPath, str);
                str = "";
            }
        }

    }
}
